<template>
  <BasicModal v-bind="$attrs" @register="register" :title="getTitle">
    <a-form ref="formRef" :model="fdata">
      <a-textarea v-model:value="fdata.Cause" rows="3" cols="16" />
    </a-form>
    <template v-slot:footer>
      <a-button type="default" @click="closeModal"> 取消 </a-button>
      <a-button type="primary" :loading="loading" @click="submitAudit">确认</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner } from '/@/components/Modal';
import { ref, reactive } from 'vue';
import { auditBack, auditPass } from '../SchemeReviewList.api';
import { useUserStore } from '/@/store/modules/user';
import { defHttp } from '/@/utils/http/axios';

const userStore = useUserStore();
const emit = defineEmits(['ok1']);
const getTitle = ref('');
let loading = ref(false);
const fdata = reactive({
  Cause: '',
  oper: '',
});
const formData = reactive<any>({
  auditCause: "",
  auditId: "",
  auditTime: "",
  auditor: "",
  createBy: "",
  createTime: "",
  editDate: "",
  editor: "",
  editorId: "",
  id: "",
  issueTime: "",
  issuer: "",
  receiveId: "",
  receiver: "",
  returnCause: "",
  returnTime: "",
  returner: "",
  schemeAtta: "",
  schemeName: "",
  status: "",
  synFlag: "",
  sysOrgCode: "",
  updateBy: "",
  updateTime: "",
  wsupTaskId: "",
  wsupTaskName: ""
});

//根据点击“不同意”、“同意”按钮显示不同的内容
const [register, { setModalProps, closeModal }] = useModalInner((data) => {
  setModalProps({
    minHeight: 40,
  });
  console.log('data.formData',data.formData)
  Object.assign(formData, data.formData);
  fdata.oper = data.str;
  if (fdata.oper == 'yes') {
    fdata.Cause = '同意';
    getTitle.value = '审核意见';
  } else {
    fdata.Cause = '';
    getTitle.value = '退回原因';
  }
});

//填写意见后，点确认按钮调用的方法
async function submitAudit() {
  if(formData.auditId&&formData.auditId.length>0){
    formData.auditId=JSON.stringify(formData.auditId);
  }
  if (fdata.oper == 'yes') {
    //复审通过调用方法
    loading.value = true;
    formData.auditMark = '审核通过';
    formData.operateMark = '监督审核通过';
    formData.finalCause = fdata.Cause;
    formData.finalAudit = userStore.getUserInfo.realname;
    formData.finalAuditId = userStore.getUserInfo.id;
    formData.returnCause = '';
    formData.returnPerson = '';
    // 初审同意更新签名
    // updateSign();

    await auditPass(formData);
    emit('ok1');
    loading.value = false;
  } else {
    //复审退回调用方法
    formData.auditMark = '审核退回';
    formData.operateMark = '监督审核退回';
    formData.returnCause = fdata.Cause;
    formData.returner = userStore.getUserInfo.realname;
    await auditBack(formData);
    emit('ok1');
  }
}
// 更新签名
function updateSign() {
  let signature = '/entrust/labEntrust/signature';
  let servestoken = '/entrust/labEntrust/serveToken';
  //获取服务token和员工票据,员工id
  defHttp.get({ url: servestoken }, { isTransformResponse: false }).then((srcs) => {
    console.log(srcs);
    //员工id
    let yaunid = srcs.cardid;
    //let yaunid = 43;
    //服务token
    let tokenServe = srcs.tokenServe;
    //let tokenServe = "eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1MDcyNzUxNDc0NDM0Mzc1NjgiLCJhdWQiOiJiYXNlIiwiaXNzIjoiemh1bnplIiwiZXhwIjoxNzAxMzQwNzgzLCJpYXQiOjE3MDEzMzI5ODN9.f4zHoRe2Z0lvE5HTmRQUqOmlYP9ygQhC_X8icNHhsuY";
    //员工票据
    let employeeTicket = srcs.employeeTicket;
    //let employeeTicket = "22a64aa46829dacaca9f4e10221de1b215be653d";
    //封装请求
    let datae = {
      businessCreateUserId: yaunid,
      businessCreateUserPositionId: '',
      content: 'flowlame=12',
      description: '',
      name: '12',
    };
    //调用签证
    //获取签名
    window.SCZZ_SIGN.open({
      token: employeeTicket, //员工票据，通过获取员工票据接口获取得到
      data: datae, //签证数据,用于签证校验，其内容见下文
      type: 'PERSONAL_SIGNATURE', //类型（个人签名:PERSONAL_SIGNATURE，单位签章:CORP_SIGNATURE），根据不同的类型打开对应的签证页面
      mobile: false, //移动端调用时需要传递
      call: async function (res) {
        console.log('更新签名弹窗返回数据', res);
        if (res.code === 1000) {
          if (res.data.imageUrl) {
            await auditPass(formData);
            emit('ok1');
            // 把签名存在数据库
            let urls = signature + '?id=' + yaunid + '&paramasa=' + res.data.imageUrl;
            defHttp.post({ url: urls }, { isTransformResponse: false });
          } else {
            // createMessage.error('签名失败，请重新签名！');
          }
        }

        //回调，接收一个结果对象，该对象里包含sdk的响应状态码code，响应信息msg，当响应code为1000时有响应数据data，响应数据内容见下文
      },
    });
  });
}
</script>

<style scoped></style>
